home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / elispman.lha / elispman / backups.texi < prev    next >
Lisp/Scheme  |  1993-05-19  |  22KB  |  590 lines

  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Emacs Lisp Reference Manual.
  3. @c Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc. 
  4. @c See the file elisp.texi for copying conditions.
  5. @setfilename ../info/backups
  6. @node Backups and Auto-Saving, Buffers, Files, Top
  7. @chapter Backups and Auto-Saving
  8.  
  9.   Backup files and auto-save files are two methods by which Emacs tries
  10. to protect the user from the consequences of crashes or of the user's
  11. own errors.  Auto-saving preserves the text from earlier in the current
  12. editing session; backup files preserve file contents prior to the
  13. current session.
  14.  
  15. @menu
  16. * Backup Files::   How backup files are made; how their names are chosen.
  17. * Auto-Saving::    How auto-save files are made; how their names are chosen.
  18. * Reverting::      @code{revert-buffer}, and how to customize what it does.
  19. @end menu
  20.  
  21. @node Backup Files, Auto-Saving, Backups and Auto-Saving, Backups and Auto-Saving
  22. @section Backup Files
  23. @cindex backup file
  24.  
  25.   A @dfn{backup file} is a copy of the old contents of a file you are
  26. editing.  Emacs makes a backup file the first time you save a buffer
  27. into its visited file.  Normally, this means that the backup file
  28. contains the contents of the file as it was before the current editing
  29. session.  The contents of the backup file normally remain unchanged once
  30. it exists.
  31.  
  32.   Backups are usually made by renaming the visited file to a new name.
  33. Optionally, you can specify that backup files should be made by copying
  34. the visited file.  This choice makes a difference for files with
  35. multiple names; it also can affect whether the edited file remains owned
  36. by the original owner or becomes owned by the user editing it.
  37.  
  38.   By default, Emacs makes a single backup file for each file edited.
  39. You can alternatively request numbered backups; then each new backup
  40. file gets a new name.  You can delete old numbered backups when you
  41. don't want them any more, or Emacs can delete them automatically.
  42.  
  43. @menu
  44. * Making Backups::     How Emacs makes backup files, and when.
  45. * Rename or Copy::     Two alternatives: renaming the old file or copying it.
  46. * Numbered Backups::   Keeping multiple backups for each source file.
  47. * Backup Names::       How backup file names are computed; customization.
  48. @end menu
  49.  
  50. @node Making Backups, Rename or Copy, Backup Files, Backup Files
  51. @subsection Making Backup Files
  52.  
  53. @defun backup-buffer
  54.   This function makes a backup of the file visited by the current
  55. buffer, if appropriate.  It is called by @code{save-buffer} before
  56. saving the buffer the first time.
  57. @end defun
  58.  
  59. @defvar buffer-backed-up
  60.   This buffer-local variable indicates whether this buffer's file has
  61. been backed up on account of this buffer.  If it is non-@code{nil}, then
  62. the backup file has been written.  Otherwise, the file should be backed
  63. up when it is next saved (if backup files are enabled).  This is a
  64. permanent local; @code{kill-local-variables} does not alter it.
  65. @end defvar
  66.  
  67. @defopt make-backup-files
  68.   This variable determines whether or not to make backup files.  If it
  69. is non-@code{nil}, then Emacs creates a backup of each file when it is
  70. saved for the first time.
  71.  
  72.   The following example shows how to change the @code{make-backup-files}
  73. variable only in the @file{RMAIL} buffer and not elsewhere.  Setting it
  74. @code{nil} stops Emacs from making backups of the @file{RMAIL} file,
  75. which may save disk space.  (You would put this code in your
  76. @file{.emacs} file.)
  77.  
  78. @example
  79. @group
  80. (add-hook 'rmail-mode-hook 
  81.           (function (lambda ()
  82.                       (make-local-variable 
  83.                        'make-backup-files)
  84.                       (setq make-backup-files nil))))
  85. @end group
  86. @end example
  87. @end defopt
  88.  
  89. @defvar backup-enable-predicate filename
  90. This variable's value is a function to be called on certain occasions
  91. to decide whether a there should be backup files for file name @var{filename}.
  92. If it returns @code{nil}, backups are disabled.  Otherwise, backups are
  93. enabled (if @code{make-backup-files} is true).
  94. @end defvar
  95.  
  96. @node Rename or Copy, Numbered Backups, Making Backups, Backup Files
  97. @subsection Backup by Renaming or by Copying?
  98. @cindex backup files, how to make them
  99.  
  100.   There are two ways that Emacs can make a backup file: 
  101.  
  102. @itemize @bullet
  103. @item
  104. Emacs can rename the original file so that it becomes a backup file, and
  105. then write the buffer being saved into a new file.  After this
  106. procedure, any other names (i.e., hard links) of the original file now
  107. refer to the backup file.  The new file is owned by the user doing the
  108. editing, and its group is the default for new files written by the user
  109. in that directory.
  110.  
  111. @item
  112. Emacs can copy the original file into a backup file, and then overwrite
  113. the original file with new contents.  After this procedure, any other
  114. names (i.e., hard links) of the original file still refer to the current
  115. version of the file.  The file's owner and group will be unchanged.
  116. @end itemize
  117.  
  118.   The first method, renaming, is the default.
  119.  
  120.   The variable @code{backup-by-copying}, if non-@code{nil}, says to use
  121. the second method, which is to copy the original file and overwrite it
  122. with the new buffer contents.  The variable @code{file-precious-flag},
  123. if non-@code{nil}, also has this effect (as a sideline of its main
  124. significance).  @xref{Saving Buffers}.
  125.  
  126.   The following two variables, when non-@code{nil}, cause the second
  127. method to be used in certain special cases.  They have no effect on the
  128. treatment of files that don't fall into the special cases.
  129.  
  130. @defvar backup-by-copying
  131.   This variable controls whether to make backup files by copying.  If it
  132. is non-@code{nil}, then Emacs always copies the current contents of the
  133. file into the backup file before writing the buffer to be saved to the
  134. file.  (In many circumstances, this has the same effect as
  135. @code{file-precious-flag}.)
  136. @end defvar
  137.  
  138. @defvar backup-by-copying-when-linked
  139.   This variable controls whether to make backups by copying for files
  140. with multiple names (hard links).  If it is non-@code{nil}, then Emacs
  141. uses copying to create backups for those files.
  142.  
  143.   This variable is significant only if @code{backup-by-copying} is
  144. @code{nil}, since copying is always used when that variable is
  145. non-@code{nil}.
  146. @end defvar
  147.  
  148. @defvar backup-by-copying-when-mismatch
  149.   This variable controls whether to make backups by copying in cases
  150. where renaming would change either the owner or the group of the file.
  151. If it is non-@code{nil} then Emacs creates backups by copying in such
  152. cases.
  153.  
  154.   The value has no effect when renaming would not alter the owner or
  155. group of the file; that is, for files which are owned by the user and
  156. whose group matches the default for a new file created there by the
  157. user.
  158.  
  159.   This variable is significant only if @code{backup-by-copying} is
  160. @code{nil}, since copying is always used when that variable is
  161. non-@code{nil}.
  162. @end defvar
  163.  
  164. @node Numbered Backups, Backup Names, Rename or Copy, Backup Files
  165. @subsection Making and Deleting Numbered Backup Files
  166.  
  167.   If a file's name is @file{foo}, the names of its numbered backup
  168. versions are @file{foo.~@var{v}~}, for various integers @var{v}, like
  169. this: @file{foo.~1~}, @file{foo.~2~}, @file{foo.~3~}, @dots{},
  170. @file{foo.~259~}, and so on.
  171.  
  172. @defopt version-control
  173.   This variable controls whether to make a single non-numbered backup
  174. file or multiple numbered backups.
  175.  
  176. @table @asis
  177. @item @code{nil}
  178. Make numbered backups if the visited file already has numbered backups;
  179. otherwise, do not.
  180.  
  181. @item @code{never}
  182. Do not make numbered backups.
  183.  
  184. @item @var{anything else}
  185. Do make numbered backups.
  186. @end table
  187. @end defopt
  188.  
  189.   The use of numbered backups ultimately leads to a large number of
  190. backup versions, which must then be deleted.  Emacs can do this
  191. automatically.
  192.  
  193. @defopt kept-new-versions
  194.   The value of this variable is the number of oldest versions to keep
  195. when a new numbered backup is made.  The newly made backup is included
  196. in the count.  The default value is 2.
  197. @end defopt
  198.  
  199. @defopt kept-old-versions
  200.   The value of this variable is the number of oldest versions to keep
  201. when a new numbered backup is made.  The default value is 2.
  202. @end defopt
  203.  
  204. @defopt dired-kept-versions
  205.   This variable plays a role in Dired's @code{dired-clean-directory}
  206. (@kbd{.}) command like that played by @code{kept-old-versions} when a
  207. backup file is made.  The default value is 2.
  208. @end defopt
  209.  
  210.   If there are backups numbered 1, 2, 3, 5, and 7, and both of these
  211. variables have the value 2, then the backups numbered 1 and 2 are kept
  212. as old versions and those numbered 5 and 7 are kept as new versions;
  213. backup version 3 is deleted.  The function @code{find-backup-file-name}
  214. (@pxref{Backup Names}) is responsible for determining which backup
  215. versions to delete, but does not delete them itself.
  216.  
  217. @defopt trim-versions-without-asking
  218.   If this variable is non-@code{nil}, then saving a file deletes excess
  219. backup versions silently.  Otherwise, it asks the user whether to delete
  220. them.
  221. @end defopt
  222.  
  223. @node Backup Names, , Numbered Backups, Backup Files
  224. @subsection Naming Backup Files
  225.  
  226.   The functions in this section are documented mainly because you can
  227. customize the naming conventions for backup files by redefining them.
  228. If you change one, you probably need to change the rest.
  229.  
  230. @defun backup-file-name-p filename
  231. This function returns a non-@code{nil} value if @var{filename} is a
  232. possible name for a backup file.  A file with the name @var{filename}
  233. need not exist; the function just checks the name.
  234.  
  235. @example
  236. @group
  237. (backup-file-name-p "foo")
  238.      @result{} nil
  239. @end group
  240. @group
  241. (backup-file-name-p "foo~")
  242.      @result{} 3
  243. @end group
  244. @end example
  245.  
  246. The standard definition of this function is as follows:
  247.  
  248. @example
  249. @group
  250. (defun backup-file-name-p (file)
  251.   "Return non-nil if FILE is a backup file \
  252. name (numeric or not)..."
  253.   (string-match "~$" file))
  254. @end group
  255. @end example
  256.  
  257. @noindent
  258. Thus, the function returns a non-@code{nil} value if the file name ends
  259. with a @samp{~}.  (We use a backslash to split the documentation
  260. string's first line into two lines in the text, but produce just one
  261. line in the string itself.)
  262.  
  263. This simple expression is placed in a separate function to make it easy
  264. to redefine for customization.
  265. @end defun
  266.  
  267. @defun make-backup-file-name filename
  268. This function returns a string which is the name to use for a
  269. non-numbered backup file for file @var{filename}.  On Unix, this is just
  270. @var{filename} with a tilde appended.
  271.  
  272. The standard definition of this function is as follows:
  273.  
  274. @example
  275. @group
  276. (defun make-backup-file-name (file)
  277.   "Create the non-numeric backup file name for FILE..."
  278.   (concat file "~"))
  279. @end group
  280. @end example
  281.  
  282. You can change the backup file naming convention by redefining this
  283. function.  In the following example, @code{make-backup-file-name} is
  284. redefined to prepend a @samp{.} as well as to append a tilde.
  285.  
  286. @example
  287. @group
  288. (defun make-backup-file-name (filename)
  289.   (concat "." filename "~"))
  290. @end group
  291.  
  292. @group
  293. (make-backup-file-name "backups.texi")
  294.      @result{} ".backups.texi~"
  295. @end group
  296. @end example
  297. @end defun
  298.  
  299. @defun find-backup-file-name filename
  300.   This function computes the file name for a new backup file for
  301. @var{filename}.  It may also propose certain existing backup files for
  302. deletion.  @code{find-backup-file-name} returns a list whose @sc{car} is
  303. the name for the new backup file and whose @sc{cdr} is a list of backup
  304. files whose deletion is proposed.
  305.  
  306.   Two variables, @code{kept-old-versions} and @code{kept-new-versions},
  307. determine which old backup versions should be kept (by excluding them
  308. from the list of backup files ripe for deletion).  @xref{Numbered
  309. Backups}.
  310.  
  311.   In this example, the value says that @file{~rms/foo.~5~} is the name
  312. to use for the new backup file, and @file{~rms/foo.~3~} is an ``excess''
  313. version that the caller should consider deleting now.
  314.  
  315. @example
  316. @group
  317. (find-backup-file-name "~rms/foo")
  318.      @result{} ("~rms/foo.~5~" "~rms/foo.~3~")
  319. @end group
  320. @end example
  321. @end defun
  322.  
  323. @c Emacs 19 feature
  324. @defun file-newest-backup filename
  325. This function returns the name of the most recent backup file for
  326. @var{filename}, or @code{nil} that file has no backup files.
  327.  
  328. Some file comparison commands use this function in order to compare
  329. a file by default with its most recent backup.
  330. @end defun
  331.  
  332. @node Auto-Saving, Reverting, Backup Files, Backups and Auto-Saving
  333. @section Auto-Saving
  334. @cindex auto-saving
  335.  
  336.   Emacs periodically saves all files that you are visiting; this is
  337. called @dfn{auto-saving}.  Auto-saving prevents you from losing more
  338. than a limited amount of work if the system crashes.  By default,
  339. auto-saves happen every 300 keystrokes, or after around 30 seconds of
  340. idle time.  @xref{Auto-Save, Auto-Save, Auto-Saving: Protection Against
  341. Disasters, emacs, The GNU Emacs Manual}, for information on auto-save
  342. for users.  Here we describe the functions used to implement auto-saving
  343. and the variables that control them.
  344.  
  345. @defvar buffer-auto-save-file-name
  346.   This buffer-local variable is the name of the file used for
  347. auto-saving the current buffer.  It is @code{nil} if the buffer
  348. should not be auto-saved.
  349.  
  350. @example
  351. @group
  352. buffer-auto-save-file-name
  353. => "/xcssun/users/rms/lewis/#files.texi#"
  354. @end group
  355. @end example
  356. @end defvar
  357.  
  358. @deffn Command auto-save-mode arg
  359.   When used interactively without an argument, this command is a toggle
  360. switch: it turns on auto-saving of the current buffer if it is off, and
  361. vice-versa.  With an argument @var{arg}, the command turns auto-saving
  362. on if the value of @var{arg} is @code{t}, a nonempty list, or a positive
  363. integer.  Otherwise, it turns auto-saving off.
  364. @end deffn
  365.  
  366. @defun auto-save-file-name-p filename
  367.   This function returns a non-@code{nil} value if @var{filename} is a
  368. string that could be the name of an auto-save file.  It works based on
  369. knowledge of the naming convention for auto-save files: a name that
  370. begins and ends with hash marks (@samp{#}) is a possible auto-save file
  371. name.  The argument @var{filename} should not contain a directory part.
  372.  
  373. @example
  374. @group
  375. (make-auto-save-file-name)
  376.      @result{} "/xcssun/users/rms/lewis/#files.texi#"
  377. @end group
  378. @group
  379. (auto-save-file-name-p "#files.texi#")
  380.      @result{} 0
  381. @end group
  382. @group
  383. (auto-save-file-name-p "files.texi")
  384.      @result{} nil
  385. @end group
  386. @end example
  387.  
  388.   The standard definition of this function is as follows:
  389.  
  390. @example
  391. @group
  392. (defun auto-save-file-name-p (filename)
  393.   "Return non-nil if FILENAME can be yielded by..."
  394.   (string-match "^#.*#$" filename))
  395. @end group
  396. @end example
  397.  
  398.   This function exists so that you can customize it if you wish to
  399. change the naming convention for auto-save files.  If you redefine it,
  400. be sure to redefine the function @code{make-auto-save-file-name}
  401. correspondingly.
  402. @end defun
  403.  
  404. @defun make-auto-save-file-name
  405.   This function returns the file name to use for auto-saving the current
  406. buffer.  This is just the file name with hash marks (@samp{#}) appended
  407. and prepended to it.  This function does not look at the variable
  408. @code{auto-save-visited-file-name}; that should be checked before this
  409. function is called.
  410.  
  411. @example
  412. @group
  413. (make-auto-save-file-name)
  414.      @result{} "/xcssun/users/rms/lewis/#backup.texi#"
  415. @end group
  416. @end example
  417.  
  418.   The standard definition of this function is as follows:
  419.  
  420. @example
  421. @group
  422. (defun make-auto-save-file-name ()
  423.   "Return file name to use for auto-saves \
  424. of current buffer..."
  425.   (if buffer-file-name
  426. @end group
  427. @group
  428.       (concat
  429.        (file-name-directory buffer-file-name)
  430.        "#"
  431.        (file-name-nondirectory buffer-file-name)
  432.        "#")
  433.     (expand-file-name
  434.      (concat "#%" (buffer-name) "#"))))
  435. @end group
  436. @end example
  437.  
  438.   This exists as a separate function so that you can redefine it to
  439. customize the naming convention for auto-save files.  Be sure to
  440. change @code{auto-save-file-name-p} in a corresponding way.
  441. @end defun
  442.  
  443. @defvar auto-save-visited-file-name
  444.   If this variable is non-@code{nil}, Emacs auto-saves buffers in
  445. the files they are visiting.  That is, the auto-save is done in the same
  446. file which you are editing.  Normally, this variable is @code{nil}, so
  447. auto-save files have distinct names that are created by
  448. @code{make-auto-save-file-name}.
  449.  
  450.   When you change the value of this variable, the value does not take
  451. effect until the next time auto-save mode is reenabled in any given
  452. buffer.  If auto-save mode is already enabled, auto-saves continue to go
  453. in the same file name until @code{auto-save-mode} is called again.
  454. @end defvar
  455.  
  456. @defun recent-auto-save-p
  457.   This function returns @code{t} if the current buffer has been
  458. auto-saved since the last time it was read in or saved.
  459. @end defun
  460.  
  461. @defun set-buffer-auto-saved
  462.   This function marks the current buffer as auto-saved.  The buffer will
  463. not be auto-saved again until the buffer text is changed again.  The
  464. function returns @code{nil}.
  465. @end defun
  466.  
  467. @defopt auto-save-interval
  468.   The value of this variable is the number of characters that Emacs
  469. reads from the keyboard between auto-saves.  Each time this many more
  470. characters are read, auto-saving is done for all buffers in which it is
  471. enabled.
  472. @end defopt
  473.  
  474. @defopt auto-save-timeout
  475.   The value of this variable is the number of seconds of idle time that
  476. should cause auto-saving.  Each time the user pauses for this long,
  477. Emacs auto-saves any buffers that need it.  (Actually, the specified
  478. timeout is multiplied by a factor depending on the size of the current
  479. buffer.)
  480. @end defopt
  481.  
  482. @defvar auto-save-hook
  483. This normal hook is run whenever an auto-save is about to happen.
  484. @end defvar
  485.  
  486. @defopt auto-save-default
  487.   If this variable is non-@code{nil}, buffers that are visiting files
  488. have auto-saving enabled by default.  Otherwise, they do not.
  489. @end defopt
  490.  
  491. @deffn Command do-auto-save &optional no-message
  492.   This function auto-saves all buffers that need to be auto-saved.
  493. This is all buffers for which auto-saving is enabled and that have
  494. been changed since the last time they were auto-saved.
  495.  
  496.   Normally, if any buffers are auto-saved, a message
  497. @samp{Auto-saving...} is displayed in the echo area while auto-saving is
  498. going on.  However, if @var{no-message} is non-@code{nil}, the message
  499. is inhibited.
  500. @end deffn
  501.  
  502. @defun delete-auto-save-file-if-necessary
  503.   This function deletes the current buffer's auto-save file if
  504. @code{delete-auto-save-files} is non-@code{nil}.  It is called every
  505. time a buffer is saved.
  506. @end defun
  507.  
  508. @defvar delete-auto-save-files
  509.   This variable is used by the function
  510. @code{delete-auto-save-file-if-necessary}.  If it is non-@code{nil},
  511. Emacs deletes auto-save files when a true save is done (in the visited
  512. file).  This saves on disk space and unclutters your directory.
  513. @end defvar
  514.  
  515. @defun rename-auto-save-file
  516.   This function adjusts the current buffer's auto-save file name if the
  517. visited file name has changed.  It also renames an existing auto-save
  518. file.  If the visited file name has not changed, this function does
  519. nothing.
  520. @end defun
  521.  
  522. @node Reverting,  , Auto-Saving, Backups and Auto-Saving
  523. @section Reverting
  524.  
  525.   If you have made extensive changes to a file and then change your mind
  526. about them, you can get rid of them by reading in the previous version
  527. of the file with the @code{revert-buffer} command.  @xref{Reverting, ,
  528. Reverting a Buffer, emacs, The GNU Emacs Manual}.
  529.  
  530. @deffn Command revert-buffer &optional check-auto-save noconfirm
  531.   This command replaces the buffer text with the text of the visited
  532. file on disk.  This action undoes all changes since the file was visited
  533. or saved.
  534.  
  535.   If the argument @var{check-auto-save} is non-@code{nil}, and the
  536. latest auto-save file is more recent than the visited file,
  537. @code{revert-buffer} asks the user whether to use that instead.
  538. Otherwise, it always uses the text of the visited file itself.
  539. Interactively, @var{check-auto-save} is set if there is a numeric prefix
  540. argument.
  541.  
  542.   When the value of the @var{noconfirm} argument is non-@code{nil},
  543. @code{revert-buffer} does not ask for confirmation for the reversion
  544. action.  This means that the buffer contents are deleted and replaced by
  545. the text from the file on the disk, with no further opportunities for
  546. the user to prevent it.
  547.  
  548.   Since reverting works by deleting the entire text of the buffer and
  549. inserting the file contents, all the buffer's markers are relocated to
  550. point at the beginning of the buffer.  This is not ``correct'', but
  551. then, there is no way to determine what would be correct.  It is not
  552. possible to determine, from the text before and after, which characters
  553. after reversion correspond to which characters before.
  554.  
  555.   If the value of the @code{revert-buffer-function} variable is
  556. non-@code{nil}, it is called as a function with no arguments to do the
  557. work.
  558. @end deffn
  559.  
  560. @defvar revert-buffer-function
  561.   The value of this variable is the function to use to revert this
  562. buffer; but if the value of this variable is @code{nil}, then the
  563. @code{revert-buffer} function carries out its default action.  Modes
  564. such as Dired mode, in which the text being edited does not consist of a
  565. file's contents but can be regenerated in some other fashion, give this
  566. variable a buffer-local value that is a function to regenerate the
  567. contents.
  568. @end defvar
  569.  
  570. @defvar revert-buffer-insert-file-contents-function
  571.   The value of this variable, if non-@code{nil}, is the function to use
  572. to insert contents when reverting this buffer.  The function receives
  573. two arguments, first the file name to use, and second, @code{t} if the
  574. user has asked to read the auto-save file.
  575. @end defvar
  576.  
  577. @deffn Command recover-file filename
  578.   This function visits @var{filename}, but gets the contents from its
  579. last auto-save file.  This is useful after the system has crashed, to
  580. resume editing the same file without losing all the work done in the
  581. previous session.
  582.  
  583.   An error is signaled if there is no auto-save file for @var{filename},
  584. or if @var{filename} is newer than its auto-save file.  If
  585. @var{filename} does not exist, but its auto-save file does, then the
  586. auto-save file is read as usual.  This last situation may occur if you
  587. visited a nonexistent file and never actually saved it.
  588. @end deffn
  589.  
  590.